package com.jtlrm.ckd.base.acitvity;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;

import com.jtlrm.ckd.R;
import com.jtlrm.ckd.app.MyApplication;
import com.jtlrm.ckd.entity.event.ActivityFinishMessage;
import com.jtlrm.ckd.mvp.model.dao.UserHelper;
import com.jtlrm.ckd.util.EventBusUtil;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.BindView;

public abstract class WebBaseActivity extends TitleBarActivity {

    @BindView(R.id.webView)
    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    }


    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_web_base);
    }

    @Override
    protected void initView() {
        WebSettings mWebSettings = webview.getSettings();
        //设置自适应屏幕，两者合用
        mWebSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        mWebSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        mWebSettings.setDisplayZoomControls(true);//隐藏原生的缩放控件
        mWebSettings.setJavaScriptEnabled(true);
        mWebSettings.setDefaultTextEncodingName("UTF-8");
        mWebSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }
        });
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webview.setWebContentsDebuggingEnabled(MyApplication.isDebug); //开启调试模式
        }
        webview.addJavascriptInterface(new AppJs(), "app");
        webview.loadUrl(getUrl());
        webview.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
    }

    protected abstract String getUrl();

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
    }

    public class AppJs {
        @JavascriptInterface
        public String getToken() {
            String token = UserHelper.getInstance(context).getToken();
            return token;
        }

        @JavascriptInterface
        public void finish() {
            if (!context.isFinishing()) {
                EventBusUtil.postEvent(new ActivityFinishMessage());
                context.finish();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        webview.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        webview.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (webview != null) {
            webview.destroy();
        }
    }

    @Override
    public void onBackPressed() {
        goback();

    }

    @Override
    protected void initTitleBar() {
        super.initTitleBar();
        titleBar.btLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });
    }

    private void goback() {
        if (webview != null && webview.canGoBack()) {
            webview.goBack();
        } else {
            super.onBackPressed();
        }
    }


}
